package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import renderer.FluidRendererCore;

import kernel.Core;

public class MainGUI extends JFrame implements ActionListener{

	private Core core;
	private FluidRendererCore rendererCore;
	
	private static final String OPEN_FILE = "OPEN_FILE";
	private static final String CLOSE = "CLOSE";
	private static final String PLAY_FORWARD = "PLAY_FORWARD";
	private static final String PLAY_BACKWARD = "PLAY_BACKWARD";
	
	private JMenuBar menuBar;
	private JMenu menuFile;
	private JMenuItem menuFileOpen;
	private JMenuItem menuFileClose;
	
	private JMenu menuPlay;
	private JMenuItem menuPlayForward;
	private JMenuItem menuPlayBackward;
	
	private int step = 0;
	private int maxStep = 0;
	public MainGUI()
	{
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setSize(300, 100);
		
		menuBar = new JMenuBar();
		
		menuFile = new JMenu("File");
		menuFileOpen = new JMenuItem("Open File");
		menuFileOpen.setActionCommand(OPEN_FILE);
		menuFileOpen.addActionListener(this);
		
		menuFile.add(menuFileOpen);
		menuFile.add(new JSeparator());
		
		menuFileClose = new JMenuItem("Close File");
		menuFileClose.setActionCommand(CLOSE);
		menuFileClose.addActionListener(this);
		
		menuFile.add(menuFileClose);
		
		menuBar.add(menuFile);
		
		menuPlay = new JMenu("Play");
		
		menuPlayForward = new JMenuItem(">>");
		menuPlayForward.setActionCommand(PLAY_FORWARD);
		menuPlayForward.addActionListener(this);
		
		menuPlay.add(menuPlayForward);
		
		menuPlayBackward = new JMenuItem("<<");
		menuPlayBackward.setActionCommand(PLAY_BACKWARD);
		menuPlayBackward.addActionListener(this);
		
		menuPlay.add(menuPlayBackward);
		
		menuBar.add(menuPlay);
		
		this.setJMenuBar(menuBar);
		
		core = new Core();
		rendererCore = new FluidRendererCore();
		this.setVisible(true);
		
	}
	
	public static void main(String[] args)
	{
		new MainGUI();
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		String actionCommand = actionEvent.getActionCommand();
		
		if(actionCommand.equals(OPEN_FILE))
		{
			JFileChooser jFileChooser = new JFileChooser();
			int option = jFileChooser.showOpenDialog(this);
			if(option == JFileChooser.APPROVE_OPTION)
			{
				try {
					core.readFile(jFileChooser.getSelectedFile());
					maxStep = core.getFrames();
					DialogRenderer dialogRenderer = new DialogRenderer(rendererCore.getCanvas());
					dialogRenderer.setVisible(true);
					System.out.println("Rendering step " + step);
					rendererCore.renderFluid(core.getMatrix(step));
					System.out.println("End Rendering step " + step);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		else if(actionCommand.equals(PLAY_FORWARD))
		{
			step++;
			if(step >= maxStep)
			{
				step = 0;
			}
			System.out.println("Rendering step " + step);
			rendererCore.renderFluid(core.getMatrix(step));
			System.out.println("End Rendering step " + step);
		}
		else if(actionCommand.equals(PLAY_BACKWARD))
		{
			step--;
			if(step < 0)
			{
				step = 0;
			}
			System.out.println("Rendering step " + step);
			rendererCore.renderFluid(core.getMatrix(step));
			System.out.println("End Rendering step " + step);
		}
		else if(actionCommand.equals(CLOSE))
		{
			System.exit(0);
		}
		
	}
}
